package partner_admin

import (
	"gitee.com/theegg/go-brick/app/controller/partner_api"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_menus"
	"gitee.com/theegg/go-brick/app/model/partner/partner_admin/partner_admin_permissions"
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/cstruct"
	"github.com/gin-gonic/gin"
	"strconv"
)

// 获取管理菜单列表
func AdminMenuList(c *gin.Context) {
	// 检验参数
	var validaParams partner_admin_menus.AdminMenuQueryParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, "bind-json-"+err.Error(), nil)
		return
	}

	// 查询数据
	data, err := partner_admin_menus.GetDataList(cstruct.StructToMap(validaParams))
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), data)
		return
	}

	for i, item := range data {
		data[i].Title = item.TitleSign
	}

	treeData := partner_admin_menus.BuildMenuListTree(data, 0, 2)

	returnData := make(map[string]interface{})

	// 获取可选上级菜单
	parentOption := make([]map[string]interface{}, 0)
	firstMenuOption := map[string]interface{}{"value": 0, "label": "-- 顶级菜单 --"}
	parentOption = append(parentOption, firstMenuOption)

	for _, item := range data {
		if item.Pid == 0 {
			parentOption = append(parentOption, map[string]interface{}{"value": item.Id, "label": item.Title})
		}
	}

	returnData["parent_option"] = parentOption
	returnData["data"] = treeData

	app.NewResponse(c).JsonReturn(true, "获取数据成功", returnData)
	return
}

// 获取菜单详情
func AdminMenuDetail(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	item, errItem := partner_admin_menus.GetItemById(id)
	if errItem != nil {
		app.NewResponse(c).JsonReturn(false, errItem.Error(), nil)
		return
	}

	data := make(map[string]interface{})
	data["id"] = item.Id
	data["pid"] = item.Pid
	data["route"] = item.Route
	data["path"] = item.Path
	data["title"] = item.Title
	data["title"] = item.Title
	data["icon"] = item.Icon

	app.NewResponse(c).JsonReturn(true, "获取数据成功", data)
	return
}

// 添加管理菜单
func AdminMenuAdd(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	var modelPam *partner_admin_menus.PartnerAdminMenus
	if id > 0 {
		modelPam, err = partner_admin_menus.GetItemById(id)
		if err != nil {
			app.NewResponse(c).JsonReturn(false, err.Error(), nil)
			return
		}
	} else {
		modelPam = new(partner_admin_menus.PartnerAdminMenus)
	}

	// 校验参数
	var validaParams partner_admin_menus.AdminMenusAddParams
	if err := c.ShouldBind(&validaParams); err != nil {
		app.NewResponse(c).JsonReturn(false, "bind-json-"+err.Error(), nil)
		return
	}

	adminMenu, err := modelPam.AddOrUpdateAdminMenu(validaParams)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), adminMenu)
		return
	}

	app.NewResponse(c).JsonReturn(true, "添加数据成功", adminMenu)
	return
}

// 删除菜单
func AdminMenuDel(c *gin.Context) {
	idStr := c.PostForm("id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	// 判定当前登陆用户是否存在
	currentAdminUser := partner_api.GetLoginPartnerAdminUser(c)

	// 只有超级管理员才可以
	role, err := currentAdminUser.Role()
	if err != nil {
		app.NewResponse(c).JsonReturn(false, app.NewError(err).Error(), nil)
		return
	}

	if !role.IsSuperRole() {
		app.NewResponse(c).JsonReturn(false, app.NewError("对不起,只有超级管理员可以删除菜单!").Error(), nil)
		return
	}

	// 删除
	ok, err := partner_admin_permissions.DeleteAdminPermission(id)
	if err != nil {
		app.NewResponse(c).JsonReturn(false, err.Error(), nil)
		return
	}

	if !ok {
		app.NewResponse(c).JsonReturn(false, "对不起，删除数据发生未知错误,请联系客服!", nil)
		return
	}

	app.NewResponse(c).JsonReturn(true, "删除成功成功", nil)
	return
}
